import urllib,urllib2,re,xbmcplugin,xbmcgui,xbmcaddon
# -*- coding: cp1254 -*-
__settings__ = xbmcaddon.Addon(id='plugin.video.tvizle')
__language__ = __settings__.getLocalizedString


def CATEGORIES():
        addDir('Film ARA','Search',6,'special://home/addons/plugin.video.tvizle/resources/images/search.png')
        addDir(__language__(30000),'http://www.izlesenefilmizle.com/',1,'special://home/addons/plugin.video.tvizle/resources/images/yeni.png')
        addDir(__language__(30001),'http://www.izlesenefilmizle.com/',2,'special://home/addons/plugin.video.tvizle/resources/images/main.jpg')

def main():
        xbmctools.addFolder(FILENAME,__language__(30011), "Search()", '')
        xbmctools.addFolder(FILENAME,__language__(30013), "Recent(url)", "http://video-klipleri.org/en-yeni-klipler")
        xbmctools.addFolder(FILENAME,__language__(30014), "Recent(url)", "http://video-klipleri.org/en-iyi-klipler")
        xbmctools.addFolder(FILENAME,__language__(30012), "Last_searched(url)", "http://video-klipleri.org/")
		
def Search():
        keyboard = xbmc.Keyboard("", 'Search', False)
        keyboard.doModal()
        if keyboard.isConfirmed():
            query = keyboard.getText()
            url = ('http://www.izlesenefilmizle.com/etiket/'+ query +'.html')
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        link=link.replace('\xf6',"o").replace('&amp;',"&").replace('\xd6',"O").replace('\xfc',"u").replace('\xdd',"I").replace('\xfd',"i").replace('\xe7',"c").replace('\xde',"s").replace('\xfe',"s").replace('\xc7',"c").replace('\xf0',"g")
        response.close()
        match=re.compile('<a href="(.*?)" class="baslik">(.*?)</a><br />').findall(link)
        thumb=re.compile('src="(.*?)" width="119" height="160" />').findall(link)
        i=0
        for url,name in match:
                addDir(name,url,7,thumb[i])
                i=i+1
def RECENT(url):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        link=link.replace('\xf6',"o").replace('&amp;',"&").replace('\xd6',"O").replace('\xfc',"u").replace('\xdd',"I").replace('\xfd',"i").replace('\xe7',"c").replace('\xde',"s").replace('\xfe',"s").replace('\xc7',"c").replace('\xf0',"g")
        response.close()
        MAINMENU(url)        
        match=re.compile('<a href="(.*?)">\n\t\t\t\t<img alt=".*?"  src="(.*?)"  alt="(.*?)" width="119" height="160"').findall(link)

        for url,thumbnail,name in match:
                addDir(name,url,7,thumbnail)
        edit=re.compile('<li><a href="(.*?)"><img alt="(.*?)" src="(.*?)" width="150" height="200" /></a></li>').findall(link)
        for url,name,thumbnail in edit:
                        addDir(name,url,7,thumbnail)
        url='http://www.izlesenefilmizle.com/en-cok-izlenen.html'
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        response.close()
		#http://www.izlesenefilmizle.com/en-cok-izlenen.html
        get=re.compile('<li><a href="(.*?)"><img alt="(.*?)" src="(.*?)" width="150" height="200" /></a></li>').findall(link)
        for url,name,thumbnail in get:
                addDir(name,url,7,thumbnail)
        page=re.compile('class="active">.*?</a><a href="(.*?)">(.*?)</a>').findall(link)
        ##
        for url,name in page:
                        addDir(name,url,1,'')
       
def ALL(url):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        link=link.replace('\xf6',"o").replace('\xd6',"O").replace('\xfc',"u").replace('\xdd',"I").replace('\xfd',"i").replace('\xe7',"c").replace('\xde',"s").replace('\xfe',"s").replace('\xc7',"c").replace('\xf0',"g")
        response.close()
        match=re.compile('<a href="http://www.izlesenefilmizle.com/film-kategori/(.*?).html" title=".*?">(.*?)</a>\n').findall(link)
        ##
        for url,name in match:
                        addDir(name,'http://www.izlesenefilmizle.com/film-kategori/' + url + '.html',3,'')
        page=re.compile('class="active">.*?</a><a href="(.*?)">(.*?)</a>').findall(link)
        ##
        for url in page:
                        addDir('>SAYFA-'+ name,url,2,'')
        
        MAINMENU(url)
def SESSION(url):
                req = urllib2.Request(url)
                req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
                response = urllib2.urlopen(req)
                link=response.read()
                link=link.replace('\xf6',"o").replace('\xd6',"O").replace('\xfc',"u").replace('\xdd',"I").replace('\xfd',"i").replace('\xe7',"c").replace('\xde',"s").replace('\xfe',"s").replace('\xc7',"c").replace('\xf0',"g")
                response.close()
                match=re.compile('<a href="(.*?)">\n\t\t\t\t<img alt="(.*?)"  src="(.*?)" width="119" height="160" />\n\t\t\t\t</a>').findall(link)
                for url,name,thumbnail in match:
                                addDir(name,url,7,thumbnail)
                page=re.compile('class="active">.*?</a><a href="(.*?)">(.*?)</a>').findall(link)
                ##
                for url,name in page:
                                addDir('>SAYFA-'+ name,url,3,'')
def TEK(url):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        link=link.replace('\xf6',"o").replace('\xd6',"O").replace('\xfc',"u").replace('\xdd',"I").replace('\xfd',"i").replace('\xe7',"c").replace('\xde',"s").replace('\xfe',"s").replace('\xc7',"c").replace('\xf0',"g")
        response.close()
        match=re.compile('\n<a href="(.*?)" title="Tek Par\xc3\xa7a" >Tek Par\xc3\xa7a</a>').findall(link)
        ##
        for url in match:
                        addDir(name,url,4,'')

def VIDEOLINKS(url):
        req = urllib2.Request(url)
        req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
        response = urllib2.urlopen(req)
        link=response.read()
        link=link.replace('\xf6',"o").replace('\xd6',"O").replace('\xfc',"u").replace('\xdd',"I").replace('\xfd',"i").replace('\xe7',"c").replace('\xde',"s").replace('\xfe',"s").replace('\xc7',"c").replace('\xf0',"g")
        response.close()
#xml okuma
        page=re.compile('value="settingsFile=(.*?)"').findall(link)
        epname= 'part'
        a=0
#Create playlist
        playList = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
        playList.clear()
#xmlAddress = #
        for url in page:
                req = urllib2.Request(url)
                req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
                response = urllib2.urlopen(req)
                link=response.read()
                link=link.replace('\xf6',"o").replace('\xd6',"O").replace('\xfc',"u").replace('\xdd',"I").replace('\xfd',"i").replace('\xe7',"c").replace('\xde',"s").replace('\xfe',"s").replace('\xc7',"c").replace('\xf0',"g")
                response.close()
                ##
                match=re.compile('<videoPath value="(.*?)"/').findall(link)
                print match
                for url in match:
                        a= a+1
                        name = epname + ' - '+str(a)
                        playList.add(url)
                        addLink( name,url,'special://home/addons/plugin.video.sinecop/resources/images/izle.png')
        xbmcPlayer = xbmc.Player()
        xbmcPlayer.play(playList)
                

        
def MAINMENU(url):
        addDir(__language__(30002),'http://www.izlesenefilmizle.com/','','special://home/addons/plugin.video.sinecop/resources/images/main.jpg')
        
def get_params():
        param=[]
        paramstring=sys.argv[2]
        if len(paramstring)>=2:
                params=sys.argv[2]
                cleanedparams=params.replace('?','')
                if (params[len(params)-1]=='/'):
                        params=params[0:len(params)-2]
                pairsofparams=cleanedparams.split('&')
                param={}
                for i in range(len(pairsofparams)):
                        splitparams={}
                        splitparams=pairsofparams[i].split('=')
                        if (len(splitparams))==2:
                                param[splitparams[0]]=splitparams[1]
                                
        return param



def addList(name,url,iconimage):
        pl=xbmc.PlayList(1)
        pl.clear()
        ok=True
        liz=xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=iconimage)
        liz.setInfo( type="Video", infoLabels={ "Title": name } )
        ok=xbmc.PlayList(1).add(handle=int(sys.argv[1]),url=url, listitem=liz)
        xbmc.Player().play(pl)
        return ok

def addLink(name,url,iconimage):
        ok=True
        liz=xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=iconimage)
        liz.setInfo( type="Video", infoLabels={ "Title": name } )
        ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=liz)
        xbmc.Player().play(liz)
        return ok


def addDir(name,url,mode,iconimage):
        u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
        ok=True
        liz=xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
        liz.setInfo( type="Video", infoLabels={ "Title": name } )
        ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
        return ok
        
              
params=get_params()
url=None
name=None
mode=None

try:
        url=urllib.unquote_plus(params["url"])
except:
        pass
try:
        name=urllib.unquote_plus(params["name"])
except:
        pass
try:
        mode=int(params["mode"])
except:
        pass

print "Mode: "+str(mode)
print "URL: "+str(url)
print "Name: "+str(name)

if mode==None or url==None or len(url)<1:
        print ""
        CATEGORIES()
elif mode==1:
        print ""+url
        RECENT(url)
elif mode==2:
        print ""+url
        ALL(url)
elif mode==3:
        print ""+url
        SESSION(url)
elif mode==4:
        print ""+url
        VIDEOLINKS(url)
elif mode==6:
        print ""+url
        Search()
elif mode==7:
        print ""+url
        TEK(url)


xbmcplugin.endOfDirectory(int(sys.argv[1]))
